package com.ky.serialport.sensor;

import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

import static com.ky.serialport.utils.RandomUtils.myRandom;

/**
 * 测试传感器数据包解析
 */
public class TimeDataBean {

    public int index = -1; //传感器序号
    public float timeVel = -1; //数值
    public int dataType = -1;  //数据包类型（是贴合数据、还是分开数据）
    public ComBean comBean;
    public int signal; //信号值
    public int power; //电量值
    public long time; //数据包时间

    public TimeDataBean() {
    }

    public TimeDataBean(ComBean comBean) {
        caculate(comBean.recData);
        time = comBean.recTime;
    }

    public TimeDataBean(byte[] recData) {
        caculate(recData);
        time = System.currentTimeMillis();
    }

    public TimeDataBean caculate(ComBean comBean){
        time = comBean.recTime;
        this.comBean = comBean;
        return caculate(comBean.recData);
    }

    public TimeDataBean caculate(byte[] buffer) {
        signal = MyFunc.getSignal(buffer[20]); //测试信号
        power = MyFunc.getPower(buffer[18], buffer[19]);//电量
        index = buffer[10];
        dataType = buffer[13];

        float miao = (float) MyFunc.twoByte2IntNoSign(buffer[14], buffer[15]);
        float hoamaio = (float) MyFunc.twoByte2IntNoSign(buffer[16], buffer[17]) * 0.1f / 1000f;
        timeVel = miao + hoamaio;
        return this;
    }

    public TimeDataBean test(int index){
        time = System.currentTimeMillis();
        signal = 200;
        power = 300 + myRandom.nextInt(120);
        timeVel = myRandom.nextInt(10) + myRandom.nextFloat();
        this.index =index;
        return this;
    }

}
